<?php
/**
 * 
 * Enter description here ...
 * @author Zijian Wang
 *
 * No problem after debugging
 */

class Location_model extends CI_Model {

	function __construct()
	{
		// Call the Model constructor
		parent::__construct();

	}


   /**
	*
	* given a location information
	* @param  $id
	* No problem after debugging
	*/

	function get_location_info($id){
		$sql = "SELECT l_name, l_latitude, l_longtitude FROM location WHERE l_id = ";
		$sql.= " '".$id."'";
		$q = $this->db->query($sql);
		$result = $q->result_array($q);
 		return $result;
	}



	/**
	*
	* given a location, return get the near location within given kilometers,default 10 kilometers.
	* @param  $id
	* @return array with default 5 rows which is in a distance of $kilometer km. 
	* No problem after debugging
	*/
	function get_location_near($l_id,$kilometer = 10,$start_rows = 0,$end_rows = 5){
		
		if($kilometer < 0)
		{
			// "Wrong Distance Input!";
			return false;
		}

		if($l_id == NULL )
		{
			// "This user doesn't exit!";
			return false;
		}

		$sql = "SELECT l_latitude,l_longtitude FROM location WHERE l_id = '".$l_id."' ";

		$sql1 = "SELECT l_latitude,l_longtitude FROM location WHERE l_id = '".$l_id."' ";
		$q = $this->db->query($sql1);
		$result1 = $q->result_array($sql1);

		$a_latitude = $result1[0]['l_latitude'];
		$a_longtitude = $result1[0]['l_longtitude'];

		$sql2 = "SELECT l_name,l_latitude,l_longtitude FROM location WHERE ";
		$sql2.= "l_id IN (SELECT L.l_id FROM location AS L WHERE ";
		$sql2.= " 12392.021 * ( ".$a_latitude."  - L.l_latitude ) * ( ".$a_latitude."  - L.l_latitude) + ";
		$sql2.="12392.021 * (".$a_longtitude."  - L.l_longtitude) * ( ".$a_longtitude."  - L.l_longtitude)";
		$sql2.=" < " .$kilometer." * ".$kilometer." )";
		$sql2.=" LIMIT ".$start_rows.",".$end_rows;
		$q2 = $this->db->query($sql2);
		$result2 = $q2->result_array($q2);

		return $result2;
	
	}

	

	/**
	 * 
	 * search location with name,use LIKE
	 * @param unknown_type $location_name
	 * @return array of location
	 * No problem after debugging
	 */
	function search_location($location_name){
		if($location_name == NULL)
		{
			// "Input can not be NULL, please retry!";
			return false;
		}
		$sql = "SELECT l_name,l_latitude,l_longtitude FROM location WHERE l_name LIKE '%".$location_name."%'";
		$q = $this->db->query($sql);
		$result = $q->result_array($q);
 		return $result;
	}

	/**
	 * Insert a new coordinate into the Database.
	 * 
	 * @param $latitude,$longitude, NO NULL and in the scope.
	 * @param $loca_name, can be NULL.
	 * Note: l_id can be created automatically by adding 1.
	 * @return false/true
	 */
	function insert_location($latitude = NUll, $longitude = NULL, $loca_name = NULL){

		if( ($latitude == NUll) or ($longitude == NULL) or ($latitude < - 90.0) or ($latitude > 90.0) or ($longitude < -180.0) or ($longitude > 180.0) )
		{
			// "Inserted coordinates are invalid!";
			return false;
		}
        $sql = "INSERT INTO location (l_latitude,l_longtitude,l_name) VALUES ";
		$sql .= " ('" . $latitude . "','" .$longitude. "','" . $loca_name."')"; 
		$result = $this->db->query($sql);
 		return $result;
	}

}

?>